import AudioManager from "../btoolkit/AudioManager";
import App from "../btoolkit/framework/App";

const { ccclass, property } = cc._decorator;


@ccclass
export default class StarPanel extends cc.Component {

    private light: cc.Node; //后面的光圈
    private huaRoot: cc.Node; //花的父节点
    private labaRoot: cc.Node;
    private lb1: cc.Node; //喇叭1
    private lb2: cc.Node; //喇叭2
    private lb3: cc.Node; //喇叭3
    private lb4: cc.Node; //喇叭4
    private circle: cc.Node; //圆圈
    private role: cc.Node; //角色节点
    private dai: cc.Node; //蓝色带子
    private caiDai: cc.ParticleSystem; //彩带粒子
    private zhiPian1: cc.ParticleSystem; //纸片粒子
    private zhiPian2: cc.ParticleSystem; //纸片粒子

    private onFinish: Function;

    static show(onFinish?: Function) {
        let instance = cc.instantiate(App.instance.resPreloader.getRes('prefabs/common/StarPanel'));
        cc.Canvas.instance.node.addChild(instance);
        instance.getComponent(StarPanel).onFinish = onFinish;
    }

    onLoad() {
        let board = this.node;
        this.light = board.getChildByName("light");
        this.huaRoot = board.getChildByName("huaRoot");
        let labaRoot = this.labaRoot = board.getChildByName("labaRoot");
        this.lb1 = labaRoot.getChildByName("lb1");
        this.lb2 = labaRoot.getChildByName("lb2");
        this.lb3 = labaRoot.getChildByName("lb3");
        this.lb4 = labaRoot.getChildByName("lb4");
        this.circle = board.getChildByName("yuan");
        this.role = board.getChildByName("role");
        this.dai = board.getChildByName("dai");
        this.light.active = this.huaRoot.active = this.labaRoot.active = this.circle.active = this.role.active = this.dai.active = false;
        //粒子特效
        this.caiDai = board.getChildByName("cd2").getComponent(cc.ParticleSystem);
        this.zhiPian1 = board.getChildByName("zp1").getComponent(cc.ParticleSystem);
        this.zhiPian2 = board.getChildByName("zp2").getComponent(cc.ParticleSystem);
    }

    start() {
        AudioManager.playFX('common/fx_BigHappy');
        this.playEffect();

        this.scheduleOnce(() => {
            cc.tween(this.node).to(0.5, { opacity: 0 }).call(() => {
                this.node.destroy();
                if (this.onFinish) {
                    this.onFinish();
                }
            }).start();
        }, 3);
    }


    ////////////  内部功能函数  ////////////
    /** 播放角色渐现、放大特效 */
    private playEffect(): void {
        //圆圈动画
        let circle = this.circle;
        circle.active = true;
        circle.stopAllActions();
        circle.scale = 0;
        circle.runAction(cc.scaleTo(0.6, 1).easing(cc.easeBackOut()));
        //蓝色彩带动画
        let delay1 = 0.2; //圆圈出现后，延迟delay1秒出现蓝色彩带
        let dai = this.dai;
        dai.active = true;
        dai.stopAllActions();
        dai.scale = 0.5;
        dai.opacity = 0;
        dai.runAction(cc.sequence(
            cc.delayTime(0.2),
            cc.callFunc(() => {
                dai.opacity = 255;
                //粒子特效
                this.caiDai.resetSystem();
                this.zhiPian1.resetSystem();
                this.zhiPian2.resetSystem();
            }),
            cc.scaleTo(0.2, 1.2),
            cc.scaleTo(0.2, 0.8),
            cc.scaleTo(0.2, 1.2),
            cc.scaleTo(0.2, 1),
        ));
        //人动画
        let delay2 = delay1 + 0.2; //圆圈出现后，延迟delay1秒出现蓝色彩带
        let role = this.role;
        role.active = true;
        role.opacity = 0;
        role.scale = 0.5;
        role.runAction(cc.sequence(
            cc.delayTime(delay2),
            cc.callFunc(() => {
                role.opacity = 255;
            }),
            cc.scaleTo(0.15, 1.2),
            cc.scaleTo(0.15, 1)
        ));
        //花动画
        let huaRoot = this.huaRoot;
        huaRoot.active = true;
        huaRoot.opacity = 0;
        huaRoot.scale = 0.7;
        huaRoot.runAction(cc.sequence(
            cc.delayTime(delay2),
            cc.callFunc(() => {
                huaRoot.opacity = 255;
            }),
            cc.scaleTo(0.2, 1.1),
            cc.scaleTo(0.2, 0.9),
            cc.scaleTo(0.3, 1.1),
            cc.scaleTo(0.3, 1)
        ));
        //喇叭动画
        this.labaRoot.active = true;
        for (let i = 1; i <= 4; i++) {
            let laba: cc.Node = this['lb' + i];
            laba.opacity = 0;
            let sign = laba.scaleX < 0 ? -1 : 1;
            laba.scaleX *= 0.1;
            laba.scaleY *= 0.1;
            laba.runAction(cc.sequence(
                cc.spawn(
                    cc.fadeIn(0.2),
                    cc.scaleTo(0.2, 1.2 * sign, 1.2),
                ),
                cc.sequence(
                    cc.scaleTo(0.3, 0.9 * sign, 0.9),
                    cc.scaleTo(0.3, 1.1 * sign, 1.1)
                ).repeatForever()
            ));
        }
    }
}
